#
# Cultural Influences
# CvCultureOverTradeRoutesEventManager By TheLopez
#

from CvPythonExtensions import *
import CvUtil

import CvEventManager
import sys
import math
import PyHelpers
import CvConfigParser
import CityUtils

gc = CyGlobalContext()	

PyPlayer = PyHelpers.PyPlayer
PyGame = PyHelpers.PyGame()


# Change this value to false if cultural borders should not go over ocean 
# plots.
# Default value is true
g_bEnableBordersOverOcean = true

# Change this value to false if cultural borders should not grow slowly.
# Default value is true
g_bEnableSlowCultureBorders = true

# Change this value to false if culture should not be carried through trade 
# routes between cities belonging to different civilizations.
# Default value is true.
g_bEnableCultureOverTradeRoutes = true

# Change this value to false if the trade route culture should be based on a
# fixed number instead of on the culture of the cities the trade route is 
# between.
# Default value is true
g_bTradeRouteCultureCityBased = true

# Change this value to false if the culture from one end of the trade route 
# should not be impeded by the culture of the city it is trying to "corrupt".
# Default value is true
g_bHomeCityCultureChallengeAlienCulture = true

# Increase or decrease this value to change the amount of culture that should
# be added to each end of a trade route. This value will be ignored if the
# value of "Trade Route Culture City Based" is true.
# Default value is 1
g_iTradeRouteCultureAmount = 1

# Set to true to print out debug messages in the logs
g_bDebug = true

def loadConfigurationData():
	global g_bEnableBordersOverOcean
	global g_bEnableSlowCultureBorders
	global g_bEnableCultureOverTradeRoutes
	global g_bTradeRouteCultureCityBased
	global g_bHomeCityCultureChallengeAlienCulture
	global g_iTradeRouteCultureAmount
		
	config = CvConfigParser.CvConfigParser("ViSa_v3.ini")

	if(config != None):
		g_bEnableBordersOverOcean = config.getboolean("Cultural Influences", "Enable Borders Over Ocean", True)
		g_bEnableSlowCultureBorders = config.getboolean("Cultural Influences", "Enable Slow Culture Borders", True)
		g_bEnableCultureOverTradeRoutes = config.getboolean("Cultural Influences", "Enable Culture Over Trade Routes", True)
		g_bTradeRouteCultureCityBased = config.getboolean("Cultural Influences", "Trade Route Culture City Based", True)
		g_bHomeCityCultureChallengeAlienCulture = config.getboolean("Cultural Influences", "Home City Culture Challenge Alien Culture", True)
		g_iTradeRouteCultureAmount = config.getint("Cultural Influences", "Trade Route Culture Amount", 1)
		
	gc.getGame().slowCultureBorders(g_bEnableSlowCultureBorders)
	gc.getGame().bordersOverOcean(g_bEnableBordersOverOcean)

# globals
###################################################
class CvCulturalInfluencesEventManager:
	def __init__(self, eventManager):
		# initialize base class

		eventManager.addEventHandler("cityDoTurn", self.onCityDoTurn)
		eventManager.addEventHandler("GameStart", self.onGameStart)
		eventManager.addEventHandler("OnLoad", self.onGameStart)

		loadConfigurationData()

			
	def onGameStart(self, argsList):
		loadConfigurationData()

		
	def onCityDoTurn(self, argsList):
		'City Production'
		objCity = argsList[0]
		iPlayer = argsList[1]
		
		if(g_bEnableCultureOverTradeRoutes):
			self.updateCityCulture(objCity)


	# Updates the culture of the city passed in depending on the configuration 
	# options	
	def updateCityCulture(self, objCity):

		iBaseCultureDivisor = 100
		
		# Return immediately if the city passed in is invalid				
		if(objCity == None):
			return []
			
		# Return immediately if the city passed in is invalid
		if(objCity.isNone()):
			return []
			
		cityList = CityUtils.getTradeCityList(objCity)
			
		if(len(cityList) == 0):
			return
		
		iBaseHomeCityCulture = objCity.getCulture(objCity.getOwner())

		for objTradeCity in cityList:
			
			if (objTradeCity and objTradeCity.getOwner() >= 0):
				iHomeCityCulture = 0
				
				iBaseTradeCityCulture = objTradeCity.getCulture(objTradeCity.getOwner())
				
				if(g_bTradeRouteCultureCityBased):
					iHomeCityCulture = iBaseHomeCityCulture/iBaseCultureDivisor
				else:
					iHomeCityCulture = g_iTradeRouteCultureAmount
					
				if(g_bHomeCityCultureChallengeAlienCulture and iBaseTradeCityCulture > 0):
					iCultureChallege = gc.getGame().getMapRand().getInt(iBaseTradeCityCulture, "Culture Challenge")

					if(iCultureChallege < iBaseHomeCityCulture):
						objTradeCity.plot().changeCulture(objCity.getOwner(), iHomeCityCulture, true)
						
						if(g_bDebug):
							CvUtil.pyPrint("%s:%s spread %s culture to %s:%s : %s < %s" %(objCity.getName(),iBaseHomeCityCulture, iHomeCityCulture, objTradeCity.getName(), iBaseTradeCityCulture, iCultureChallege, iBaseHomeCityCulture))
					else:
						if(g_bDebug):
							CvUtil.pyPrint("%s:%s could not spread its culture to %s:%s : %s < %s" %(objCity.getName(),iBaseHomeCityCulture, objTradeCity.getName(), iBaseTradeCityCulture, iCultureChallege, iBaseHomeCityCulture))
					
				else:
					objTradeCity.plot().changeCulture(objCity.getOwner(), iHomeCityCulture, true)

